#include "logistics.h"
#include "houqin.h"
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

Goods goods[GOODS_MAX];
Loss_Goods loss_goods[LOSS_GOODS_MAX];
size_t g_goods_cnt;
size_t g_loss_goods_cnt;

bool login_logistics(void)
{
	printf("请输入ID：");
	char hou_qin_id[11];
	get_str(hou_qin_id,sizeof(hou_qin_id));
	if(0 == strlen(hou_qin_id))
	{
		print_sec("ID格式有误！登录失败！",0.75);
		return false;
	}
	int index = query_hou_qin_id(hou_qin_id);
	if(-1 == index)
	{
		print_sec("未查找到该ID，登录失败！",0.75);
		return false;
	}

	printf("请输入密码：");
	char hou_qin_password[15];
	get_str(hou_qin_password,sizeof(hou_qin_password));
	if(0 != strcmp(hou_qin_password,hou_qin[index].password))
	{
		print_sec("密码错误！",0.75);
		return false;
	}

	print_sec("登录成功！",0.75);
	return true;
}

int query_goods_id(const char* id)
{
	for(int i=0; i<g_goods_cnt; i++)
	{
		if(0 == strcmp(goods[i].id,id))
			return i;
	}
	return -1;
}

double get_price(void)
{	printf("请输入商品单价：");
	char price[11];
	get_str(price,sizeof(price));
	if(strlen(price) != strspn(price,STR_FLOAT))
	{
		return -1;
	}
	// 判断小数点个数是否大于１
	bool flag = false;
	for(int i=0; i<11; i++)
	{
		if('.' == *(price+i))
		{
			if(flag)
			{
				return -1;
			}
			flag = true;
		}

	}
	return str_to_double(price);
}

void add_goods(void)
{
	// 判断商品数量是否已满
	if(g_goods_cnt >= GOODS_MAX)
	{
		print_sec("商品数量已满，无法添加！",0.75);
		return;
	}

	// 输入id
	printf("请输入商品ID编号：");
	get_str(goods[g_goods_cnt].id,sizeof(goods[g_goods_cnt].id));
	if(0 == strlen(goods[g_goods_cnt].id) || 10 <= strlen(goods[g_goods_cnt].id) || strlen(goods[g_goods_cnt].id) != strspn(goods[g_goods_cnt].id,STR_DIGIT))
	{
		print_sec("ID编号格式有误！添加失败！",0.75);
		return;
	}
	// 检查id是否被占用
	if(-1 < query_goods_id(goods[g_goods_cnt].id))
	{
		print_sec("该商品ID已存在，添加失败！",0.75);
		return;
	}

	printf("请输入商品名称:");
	get_str(goods[g_goods_cnt].name,sizeof(goods[g_goods_cnt].name));
	if(0 == strlen(goods[g_goods_cnt].name))
	{
		print_sec("错误！商品名称不能为空！",0.75);
		return;
	}

	double price = get_price();
	if(-1 == price)
	{
		print_sec("单价输入格式错误！",0.75);
		return;
	}
	goods[g_goods_cnt].price = price;

	printf("请输入商品数量：");
	char nums[11];
	get_str(nums,sizeof(nums));
	if(strlen(nums) != strspn(nums,STR_DIGIT))
	{
		print_sec("数量输入格式错误！",0.75);
		return;
	}
	goods[g_goods_cnt].nums = str_to_n(nums);

	g_goods_cnt++;
	print_sec("添加成功！",0.75);
	return;
}

void ban_goods(void)
{
	if(0 == g_goods_cnt)
	{
		print_sec("无任何商品！",0.75);
		return;
	}

	char id[11];
	printf("请输入要下架的商品ID编号：");
	get_str(id,sizeof(id));
	//检查id是否存在
	int index = query_goods_id(id);
	if(-1 == index)
	{
		print_sec("商品ID编号不存在！",0.75);
		return;
	}

	if(yes_or_no())
	{
		goods[index] = goods[g_goods_cnt-1];
		g_goods_cnt--;
		print_sec("下架成功！",0.75);
		return;
	}
	else
	{
		print_sec("取消下架！",0.75);
		return;
	}
}

void add_goods_nums(void)
{
	if(0 == g_goods_cnt)
	{
		print_sec("无任何商品！",0.75);
		return;
	}

	printf("请输入要添加数量的商品ID编号：");
	char id[11];
	get_str(id,sizeof(id));
	//检查id是否存在
	int index = query_goods_id(id);
	if(-1 == index)
	{
		print_sec("商品ID编号不存在！",0.75);
		return;
	}

	printf("请输入要添加的商品数量：");
	char add_nums[11];
	get_str(add_nums,sizeof(add_nums));
	if(strlen(add_nums) != strspn(add_nums,STR_DIGIT))
	{
		print_sec("数量输入格式错误！",0.75);
		return;
	}
	goods[index].nums += str_to_n(add_nums);
	print_sec("添加成功！",0.75);
	return;

}

void update_goods(void)
{
	printf("请输入要修改的商品ID编号：");
	char id[11];
	get_str(id,sizeof(id));
	int index = query_goods_id(id);
	if(-1 == index)
	{
		print_sec("未查找到该商品ID编号",0.75);
		return;
	}

	printf("请输入新的商品名：");
	char name[20];
	get_str(name,sizeof(name));
	if(0 == strlen(name))
	{
		print_sec("错误！商品名称不能为空！",0.75);
		return;
	}

	double price = get_price();
	if(-1 == price)
	{
		print_sec("单价输入格式错误！",0.75);
		return;
	}

	printf("请输入商品数量：");
	char nums[11];
	get_str(nums,sizeof(nums));
	if(strlen(nums) != strspn(nums,STR_DIGIT))
	{
		print_sec("数量输入格式错误！",0.75);
		return;
	}
	strcpy(goods[index].name,name);
	goods[index].price = price;
	goods[index].nums = str_to_n(nums);
	print_sec("修改成功！",0.75);
	return;
}

void find_goods(void)
{
	if(0 == g_goods_cnt)
	{
		print_sec("无任何商品！",0.75);
		return;
	}
	printf("请输入要查找的商品ID编号：");
	char id[10];
	get_str(id,sizeof(id));
	int index = query_goods_id(id);
	if(-1 == index)
	{
		print_sec("未查找到该商品ID编号",0.75);
		return;
	}

	printf("编号：%-15s名称：%-21s单价：%.2lf元\t\t数量：%-15u\n",
			goods[index].id,
			goods[index].name,
			goods[index].price,
			goods[index].nums);
	anykey_continue();
	return;
}

void need_goods(void)
{
	for(int i=0; i<g_goods_cnt; i++)
	{
		if(GOODS_NEED >= goods[i].nums)
		{
			printf("编号：%-15s数量：%-10u单价(元)：%-15.2lf名称：%-21s\n",
				goods[i].id,
				goods[i].nums,
				goods[i].price,
				goods[i].name);
		}
	}
	anykey_continue();
	return;
}

void add_loss_goods(void)
{
	if(0 == g_goods_cnt)
	{
		print_sec("无任何商品！",0.75);
		return;
	}

	printf("请输入商品ID编号：");
	get_str(loss_goods[g_loss_goods_cnt].id,sizeof(loss_goods[g_loss_goods_cnt].id));
	int index = query_goods_id(loss_goods[g_loss_goods_cnt].id);
	if(-1 == index)
	{
		print_sec("未查找到该商品ID编号",0.75);
		return;
	}

	printf("请输入损坏商品数量：");
	char nums[11];
	get_str(nums,sizeof(nums));
	if(strlen(nums) != strspn(nums,STR_DIGIT) || goods[index].nums < str_to_n(nums) || 0 == str_to_n(nums))
	{
		print_sec("数量输入格式错误！",0.75);
		return;
	}
	
	printf("请输入原因(十个字以内)：");
	get_str(loss_goods[g_loss_goods_cnt].reason,sizeof(loss_goods[g_loss_goods_cnt].reason));
	if(0 == strlen(loss_goods[g_loss_goods_cnt].reason))
	{
		print_sec("原因格式输入错误！",0.75);
		return;
	}

	strcpy(loss_goods[g_loss_goods_cnt].name,goods[index].name);
	loss_goods[g_loss_goods_cnt].price = goods[index].price;
	loss_goods[g_loss_goods_cnt].nums = str_to_n(nums);
	goods[index].nums -= str_to_n(nums);

	g_loss_goods_cnt++;
	print_sec("添加成功！",0.75);
	puts("--------商品损失详情--------");
	show_loss_goods();
	return;
}

void show_loss_goods(void)
{
	for(int i=0; i<g_loss_goods_cnt; i++)
	{
		printf("编号：%s\n名称：%s\n单价：%.2lf\n数量：%u\n原因：%s\n\n",
				loss_goods[i].id,
				loss_goods[i].name,
				loss_goods[i].price,
				loss_goods[i].nums,
				loss_goods[i].reason);
	}
	anykey_continue();
	return;
}

void menu_goods(void)
{
	if(!login_logistics()) return;
	for(;;)
	{
		system("clear");
		puts("*****商品信息管理*****");
		puts("1、添加一种商品");
		puts("2、增加商品数量");
		puts("3、下架一种商品");
		puts("4、修改商品信息");
		puts("5、查找商品");
		puts("6、查看需要补货的商品");
		puts("7、报损");
		puts("8、返回");
		switch(get_cmd('1','8'))
		{
			case '1': add_goods(); break;
			case '2': add_goods_nums(); break;
			case '3': ban_goods(); break;
			case '4': update_goods(); break;
			case '5': find_goods(); break;
			case '6': need_goods(); break;
			case '7': add_loss_goods(); break;
			case '8': return;
		}
	}
}
